package com.alibaba.aone.casestudy.job;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Failure;
import com.alibaba.aone.casestudy.common.entity.FailureRpAsignGroup;
import com.alibaba.aone.casestudy.common.entity.FailureTime;
import com.alibaba.aone.casestudy.common.utils.MessageUtil;


import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.aone.casestudy.service.FailureTimeService;
import com.alibaba.aone.casestudy.service.GroupService;


/**
 * 类FailureJob.java的实现描述：TODO 类实现描述
 * 
 * @author yongchun.chengyc 2011-5-5 上午10:23:25
 */
public class FailureJob {

	private static Logger logger = LoggerFactory.getLogger(TestJob.class);
	@Autowired
	private FailureTimeService failureTimeService;
	@Autowired
	private FailureService failureService;
	@Autowired
	private GroupService groupService;

	/**
	 * 获得预定故障分析日期
	 */
	private Long getScheduledFailureAnalysisTime(String failureNumber) {
		int rank;
		Date gmtStart;
		Long analysisTime = 0l;
		List<FailureTime> failureTimes = failureTimeService
				.getFailureTime(failureNumber);

		for (FailureTime failureTime : failureTimes) {
			rank = failureTime.getRank();
			gmtStart = failureTime.getGmtStart();
			switch (rank) {
			case 1:
			case 2:
				analysisTime = gmtStart.getTime() + 3 *1000* 60 * 60 * 24; //事故类和A类 预定故障分析日期=发生日期+3

				break;
			case 3:
				analysisTime = gmtStart.getTime() + 7*1000* 60 * 60 * 24; //B类  预定故障分析日期=发生日期+7  此处没有考虑免分析

				break;
			case 4:
				analysisTime = gmtStart.getTime() + 14 * 60 * 60 * 24;   //C类  预定故障分析日期=发生日期+14  此处没有考虑免分析

				break;
			default:

				break;
			}

		}

		return analysisTime;
	}

	/**
	 * 发送消息
	 */
	public void job() {

		String failureNumber = null;
		// 获得当前时间
		Date now = new Date();
		Long nowDay = now.getTime();

		
		List<Failure> failures = failureService.getAllFailures();

		for (Failure failure : failures) {
			failureNumber = failure.getNumber();

			
			List<FailureRpAsignGroup> groups = groupService.getGroups(failureNumber);
			for(FailureRpAsignGroup group:groups){
			String rpMan = group.getRpMan();                                  // 获得故障的负责人

			// 此处需要调用api得到邮箱，但我们的数据库存放的所有的人都是他的邮箱前缀  ，此处需要代码重构

			// 获得预定故障分析日期
			Long analysisTime = this
					.getScheduledFailureAnalysisTime(failureNumber);

			// 提醒时间
			SimpleDateFormat format = new SimpleDateFormat("yyyy");
			String year = format.format(now);
			int day = now.getDay() + 2;

			String time = year + "年" + day + "日 ";

			//下面的地址为测试地址，需要变更
			if (((analysisTime - nowDay) / 1000 / 60 / 60 / 24 <= 2)&&((analysisTime - nowDay) / 1000 / 60 / 60 / 24 >=0)) {
				String message = rpMan + "你有一个故障《--" + failure.getTitle()
						+ "--》需要分析，请在" + time + "前完成分析"+
						"<a href=\"http://casestudy.alibaba-inc.com:8081/failureDetailView.htm?number=failureNumber\" />"
						+"《casestudy测试，测试人程永春》括号内的为可删除的";
				
				MessageUtil.sendWangwangMessage(rpMan, message);
				
				System.out.println(rpMan + "你有一个故障《--" + failure.getTitle()
						+ "--》需要分析，请在" + time + "前完成分析"+"casestudy测试，测试人程永春");
			}else if((analysisTime - nowDay) / 1000 / 60 / 60 / 24 <0){
				String message =rpMan + "你有一个故障<--" + failure.getTitle()
				+ "-->已过期，请完成分析"+
				"<a href=\"http://casestudy.alibaba-inc.com:8081/failureDetailView.htm?number=failureNumber\" />"
				+"《casestudy测试，测试人程永春》括号内的为可删除的";
				MessageUtil.sendWangwangMessage(rpMan, message);
				
				System.out.println(rpMan + "你有一个故障<--" + failure.getTitle()
				+ "-->已过期，请完成分析"+"casestudy测试，测试人程永春");
				
			}
		}
	}

	}

}
